﻿using Bilibili.Model.Common;
using Bilibili.Model.Resource;
using PCLUntils.Json;
using System;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class UpdateClient : ClientBase
    {
        public async Task<ReturnModel<VersionInfo>> LatestVerInfo(PackageVersion version)
        {
            try
            {
                var url = version == PackageVersion.Xamarin ? "https://michael-eddy.github.io/config/xamarin-bilibili.json" : "https://michael-eddy.github.io/config/avalonia-bilibili.json";
                string stringContent = await ApiClient.GetResults(url);
                var model = stringContent.ParseObject<VersionInfo>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<VersionInfo>(ex);
            }
        }
        public async Task<ReturnModel> GetRealUrl(string url)
        {
            try
            {
                string realUrl = string.Empty;
                var result = await ApiClient.GetResults($"https://api.pingping6.com/tools/lanzou/?url={url}");
                var info = result.ToJObject();
                if (info != null && Convert.ToInt32(info["code"].ToString()) == 1)
                    realUrl = info["file"].ToString();
                if (string.IsNullOrEmpty(realUrl))
                    return BuildSuccessResult(realUrl);
                else
                    return BuildFailedResult();
            }
            catch (Exception ex)
            {
                return BuildExceptionResult(ex);
            }
        }
    }
}